/*to do list
file type check*/

//#include <stdio.h>
//extern "C" { 
//int drinuse = -1;
#include <stdlib.h>
#include <pspkernel.h>
#include <pspctrl.h>
#include "directory.h"
#include "fileio.h"
#include "../States/State.h"

#include "../Charecter/ChDraw.h"


Dirlist * dirlist = NULL;
int dirlist_size = 0;
int dir_selected = 0;

unsigned char* filebuffer = NULL;
int filebuffersize = 0;

void build_path(unsigned char *output, const unsigned char *root, const unsigned char *path, int append)
{
	unsigned char *output_bak;
	int p = 0;
	int s = 0;
	int up = 0;
	if(output == NULL)
	{
		return;
	}
	output_bak = output;
	if(path != NULL && path[0] == '.')
	{
		if(path[1] == '.')
			up = 1;
		else
			path = NULL;
	}
	while(*root != 0)
	{
		if(up && *root == '/' && (*(root+1) != 0))
			s = p;
		*output++ = *root++;
		p++;
	}
	
	if(up)
	{
		output_bak[s + 1] = 0;
		return;
	}

	if(*(root-1) != '/')
	{
		*output++ = '/';
	}

	if(path != NULL)
	while(*path != 0)
	{
		*output++ = *path++;
	}
	if(append)
		*output++ = '/';

	*output++ = 0;
}

void InitDirectory()
{
	memcpy(curfilepath, INIT_DIR, sizeof(INIT_DIR));
	opendirectory(curfilepath, curfilepath, NULL);
}

int DrawDirectory(unsigned char* filepath)
{
	if(dirlist == NULL || dirlist_size <= 0)
	{
		//printf("No directory opened now!\n");
		return -1;
	}
	DrawString(DIR_X, DIR_Y - DIR_HEIGHT, filepath, NAME_LEN_MAX, DIR_COLOR, DIR_SIZE);	
	int count  = 0;
	int x0 = DIR_X;
	int y0 = DIR_Y;
	while(count < dirlist_size)
	{
		int offset = 0;
		unsigned char tbuf[NAME_LEN_MAX+2];
		memset(tbuf, 0, NAME_LEN_MAX+2);
		if(IS_DIR(dirlist[count].dirmode))
		{			
			offset += sprintf(tbuf, "<%s>", dirlist[count].dirname);
		}
		else
		{
			
			offset += sprintf(tbuf, "%s", dirlist[count].dirname);
			if(count == dir_selected)
			{			
				offset += sprintf(tbuf + offset,"   size:%d;", dirlist[count].dirsize);
			}
		}
		if(count == dir_selected)
	  	sprintf(tbuf + offset,"%s"," <=======");
    DrawString(x0, y0, tbuf, NAME_LEN_MAX, DIR_COLOR, DIR_SIZE);	  	
		y0+= DIR_HEIGHT;
		count++;
	}
	return 0;
}

int UpdateDirectory(unsigned char* filepath, int save)
{
	if(IsPressed(PSP_CTRL_CIRCLE))
	{
		if(IS_DIR(dirlist[dir_selected].dirmode))
		{
			opendirectory(filepath, filepath, dirlist[dir_selected].dirname);
			dir_selected = 0;
		}
		else
		{
			if(!save)
			{
				txtlen = ReadFile(filepath, dirlist[dir_selected].dirname, txtbuffer);			
				SetState(STATE_INITNOTPAD);	
			}
			else				
				SetState(STATE_SAVE_INPUT);
				//printf("Open %s successfully!Id: %d\n", dirlist[dir_selected].dirname, fileinuse);
		}
	}
	if(IsPressed(PSP_CTRL_UP))
	{
			dir_selected--;
			if(dir_selected<0)
				dir_selected = 0;
	}
	if(IsPressed(PSP_CTRL_DOWN))
	{
			dir_selected++;
			if(dir_selected>=dirlist_size)
				dir_selected = dirlist_size-1;
	}
}

int opendirectory(unsigned char* filepath, const unsigned char *location, const unsigned char *directoryname)
{
	build_path(filepath, location, directoryname, 0);
	//printf("Opening %s!\n", curfilepath);
		
	int drinuse = sceIoDopen(filepath);
	if(drinuse < 0)
	{
//		printf("Opening %s failed!\n", curfilepath);
		return -1;
	}
//	else
//	{
//		printf("Opening %s successfully!\n", curfilepath);
//	}
	
	SceIoDirent sid;
	int result = 0;
	memset(&sid, 0, sizeof(SceIoDirent));
	while(sceIoDread(drinuse, &sid))
	{
		result++;
		memset(&sid, 0, sizeof(SceIoDirent));
	}
	//printf("Get directory number\n");
	dirlist_size = result;
	//printf("close %d\n", drinuse);
	if(sceIoDclose(drinuse) < 0)
	{
		//printf("close dir failed!");
		return -1;
	}
	drinuse = sceIoDopen(filepath);
	//printf("open %s!again\n", curfilepath);
	if(dirlist != NULL)
	{
		//printf("free dirlist start %d \n", (int)dirlist);
		//delete(dirlist);
		free((void *)dirlist);
		//printf("free dirlist end\n");
		dirlist = NULL;
//		printf("realloc dirlist \n");
//		dirlist = (Dirlist*) realloc(dirlist, dirlist_size*sizeof(Dirlist));
	}
//	else
//	{
//		printf("malloc dirlist \n");
//		dirlist = (Dirlist*) malloc(dirlist_size*sizeof(Dirlist));
//	}
	//printf("free dirlist succesful\n");
	//printf("dirlist_size %d \n", dirlist_size);
	dirlist = (Dirlist*) malloc(dirlist_size*sizeof(Dirlist));
	//dirlist = new Dirlist[dirlist_size];
	//printf("malloc dirlist %d\n", (int)dirlist);

	int count = 0;
	memset(&sid, 0, sizeof(SceIoDirent));
	memset(&dirlist[0], 0, sizeof(Dirlist));
	while(sceIoDread(drinuse, &sid))
	{
		strcpy(dirlist[count].dirname, sid.d_name);
		dirlist[count].dirmode = FIO_S_ISDIR(sid.d_stat.st_mode)?DIR_TYPE_DIR:DIR_TYPE_FILE;
		dirlist[count].dirtimeday = ((sid.d_stat.st_ctime.year - 1980) << 9) + (sid.d_stat.st_ctime.month << 5) + sid.d_stat.st_ctime.day;
		dirlist[count].dirtimesec = (sid.d_stat.st_ctime.hour << 11) + (sid.d_stat.st_ctime.minute << 5) + sid.d_stat.st_ctime.second / 2;
		dirlist[count].dirsize = 0;
		//dirlist[count++] = sid;
		if(!IS_DIR(dirlist[count].dirmode))
		{
			dirlist[count].dirsize = GetFileSize(filepath, dirlist[count].dirname);
		}
		if(count < dirlist_size - 1)
		{
			memset(&sid, 0, sizeof(SceIoDirent));
			memset(&dirlist[++count], 0, sizeof(Dirlist));
		}
	}
	//printf("get dirlist\n");
	//showdirectory();
	
	if(drinuse >= 0)
	{
		sceIoDclose(drinuse);
	}
	return 0;
}

//}
